//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CapHelper.h"
using namespace Cephei::QL::Models::Shortrate::Calibrationhelpers;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Models/CalibrationHelper.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Models;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::CCapHelper (Cephei::QL::Times::IPeriod^ length, Cephei::QL::IQuote^ volatility, Cephei::QL::Indexes::IIborIndex^ index, QL::Times::FrequencyEnum fixedLegFrequency, Cephei::QL::Times::IDayCounter^ fixedLegDayCounter, Boolean includeFirstSwaplet, Cephei::QL::Termstructures::IYieldTermStructure^ termStructure, Microsoft::FSharp::Core::FSharpOption<QL::Models::CalibrationHelper::CalibrationErrorTypeEnum>^ errorType, Cephei::QL::IPricingEngine^ QL_Pricer) : CCalibrationHelper(CCapHelper::typeid)
{
    CPeriod^ _Clength;
    CQuote^ _Cvolatility;
    CIborIndex^ _Cindex;
    CDayCounter^ _CfixedLegDayCounter;
    CYieldTermStructure^ _CtermStructure;
    try
    {
#ifdef HANDLE
        _phCapHelper = NULL;
#endif
        _Clength = safe_cast<CPeriod^> (length);
        _Clength->Lock();
        QuantLib::Period& _length = static_cast<QuantLib::Period&> (_Clength->GetReference ()); 
        _Cvolatility = safe_cast<CQuote^> (volatility);
        _Cvolatility->Lock();
        Handle<QuantLib::Quote>& _volatility = static_cast<Handle<QuantLib::Quote>&> (_Cvolatility->GetHandle ()); 
        _Cindex = safe_cast<CIborIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _index = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_Cindex->GetShared ()); 
        QuantLib::Frequency _fixedLegFrequency = (QuantLib::Frequency)fixedLegFrequency ;
        _CfixedLegDayCounter = safe_cast<CDayCounter^> (fixedLegDayCounter);
        _CfixedLegDayCounter->Lock();
        QuantLib::DayCounter& _fixedLegDayCounter = static_cast<QuantLib::DayCounter&> (_CfixedLegDayCounter->GetReference ()); 
        bool _includeFirstSwaplet = (bool)ValueHelper::Convert (includeFirstSwaplet);
        _CtermStructure = safe_cast<CYieldTermStructure^> (termStructure);
        _CtermStructure->Lock();
        Handle<QuantLib::YieldTermStructure>& _termStructure = static_cast<Handle<QuantLib::YieldTermStructure>&> (_CtermStructure->GetHandle ()); 
        QuantLib::CalibrationHelper::CalibrationErrorType _errorType = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Models::CalibrationHelper::CalibrationErrorTypeEnum>::IsSome::get (errorType) ? (QuantLib::CalibrationHelper::CalibrationErrorType)errorType->Value : QuantLib::CalibrationHelper::CalibrationErrorType::RelativePriceError); //10
        _ppCapHelper = new boost::shared_ptr<QuantLib::CapHelper> (new QuantLib::CapHelper ( _length,  _volatility,  _index,  _fixedLegFrequency,  _fixedLegDayCounter,  _includeFirstSwaplet,  _termStructure,  _errorType ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppCapHelper)->setPricingEngine (_QL_Pricer);
        SetCalibrationHelper (boost::dynamic_pointer_cast<QuantLib::CalibrationHelper> (*_ppCapHelper));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Clength != nullptr) _Clength->Unlock();
        if (_Cvolatility != nullptr) _Cvolatility->Unlock();
        if (_Cindex != nullptr) _Cindex->Unlock();
        if (_CfixedLegDayCounter != nullptr) _CfixedLegDayCounter->Unlock();
        if (_CtermStructure != nullptr) _CtermStructure->Unlock();
    }
}
Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::CCapHelper (boost::shared_ptr<QuantLib::CapHelper>& childNative, Object^ owner) : CCalibrationHelper(CCapHelper::typeid)
{
#ifdef HANDLE
	_phCapHelper = NULL;
#endif
	_ppCapHelper = &childNative;
    _ppCalibrationHelper = new boost::shared_ptr<QuantLib::CalibrationHelper> (boost::dynamic_pointer_cast<QuantLib::CalibrationHelper> (*_ppCapHelper));
}
Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::CCapHelper (QuantLib::CapHelper& childNative, Object^ owner) : CCalibrationHelper(CCapHelper::typeid)
{
#ifdef HANDLE
	_phCapHelper = NULL;
#endif
	_ppCapHelper = new boost::shared_ptr<QuantLib::CapHelper> (&childNative);
    _ppCalibrationHelper = new boost::shared_ptr<QuantLib::CalibrationHelper> (boost::dynamic_pointer_cast<QuantLib::CalibrationHelper> (*_ppCapHelper));
    _CapHelperOwner = owner;
    _CalibrationHelperOwner = owner;
}

Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::CCapHelper (CCapHelper^ copy) : CCalibrationHelper(CCapHelper::typeid)
{
#ifdef HANDLE
	_phCapHelper = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCapHelper = new boost::shared_ptr<QuantLib::CapHelper> (copy->GetShared());
        _ppCalibrationHelper = new boost::shared_ptr<QuantLib::CalibrationHelper> (boost::dynamic_pointer_cast<QuantLib::CalibrationHelper> (*_ppCapHelper));
    }
}
Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::CCapHelper (System::Type^ t) : CCalibrationHelper(CCapHelper::typeid)
{
#ifdef HANDLE
	_phCapHelper = NULL;
#endif
	if (!t->IsSubclassOf(CCapHelper::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::CCapHelper (QuantLib::Handle<QuantLib::CapHelper>& childNative, Object^ owner)  : CCalibrationHelper(CCapHelper::typeid)
{
	_phCapHelper = &childNative;
	_ppCapHelper = &static_cast<boost::shared_ptr<QuantLib::CapHelper>>(childNative.currentLink());
    _ppCalibrationHelper = new boost::shared_ptr<QuantLib::CalibrationHelper> (boost::dynamic_pointer_cast<QuantLib::CalibrationHelper> (*_ppCapHelper));
    _CapHelperOwner = owner;
}
Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::CCapHelper (QuantLib::Handle<QuantLib::CapHelper> childNative)  : CCalibrationHelper(CCapHelper::typeid)
{
	_phCapHelper = &childNative;
	_ppCapHelper = &static_cast<boost::shared_ptr<QuantLib::CapHelper>>(childNative.currentLink());
    _ppCalibrationHelper = new boost::shared_ptr<QuantLib::CalibrationHelper> (boost::dynamic_pointer_cast<QuantLib::CalibrationHelper> (*_ppCapHelper));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::CCapHelper (QuantLib::CapHelper childNative)  : CCalibrationHelper(CCapHelper::typeid)
{
#ifdef HANDLE
	_phCapHelper = NULL;
#endif
	_ppCapHelper = new boost::shared_ptr<QuantLib::CapHelper> (new QuantLib::CapHelper (childNative));
    _ppCalibrationHelper = new boost::shared_ptr<QuantLib::CalibrationHelper> (boost::dynamic_pointer_cast<QuantLib::CalibrationHelper> (*_ppCapHelper));
}
#endif

Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::~CCapHelper ()
{
    if (_ppCapHelper != NULL)
    {
	    delete _ppCapHelper;
        _ppCapHelper = NULL;
    }
}
Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::!CCapHelper ()
{
    if (_ppCapHelper != NULL)
    {
	    delete _ppCapHelper;
    }
}
QuantLib::CapHelper& Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::GetReference ()
{
    if (_ppCapHelper == NULL) throw gcnew NativeNullException ();
	return **_ppCapHelper;
}
boost::shared_ptr<QuantLib::CapHelper>& Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::GetShared ()
{
    if (_ppCapHelper == NULL) throw gcnew NativeNullException ();
	return *_ppCapHelper;
}
QuantLib::CapHelper* Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::GetPointer ()
{
    if (_ppCapHelper == NULL) throw gcnew NativeNullException ();
	return &**_ppCapHelper;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CapHelper>& Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::GetHandle ()
{
	if (_phCapHelper == NULL)
	{
		_phCapHelper = new Handle<QuantLib::CapHelper> (*_ppCapHelper);
	}
	return *_phCapHelper;
}
#endif
bool Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::HasNative () 
{
	return (_ppCapHelper != NULL);
}

Double Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::BlackPrice (Double volatility)
{
    try
    {
        QuantLib::Volatility _volatility = (QuantLib::Volatility)ValueHelper::Convert (volatility);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCapHelper)->blackPrice ( _volatility );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper::ModelValue::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppCapHelper)->modelValue ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Shortrate::Calibrationhelpers::ICapHelper^ Cephei::QL::Models::Shortrate::Calibrationhelpers::CCapHelper_Factory::Create (Cephei::QL::Times::IPeriod^ length, Cephei::QL::IQuote^ volatility, Cephei::QL::Indexes::IIborIndex^ index, QL::Times::FrequencyEnum fixedLegFrequency, Cephei::QL::Times::IDayCounter^ fixedLegDayCounter, Boolean includeFirstSwaplet, Cephei::QL::Termstructures::IYieldTermStructure^ termStructure, Microsoft::FSharp::Core::FSharpOption<QL::Models::CalibrationHelper::CalibrationErrorTypeEnum>^ errorType, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CCapHelper ( length,  volatility,  index,  fixedLegFrequency,  fixedLegDayCounter,  includeFirstSwaplet,  termStructure,  errorType,  QL_Pricer);
}
